package jp.co.nes.awssupport.business.logic.description;

import jp.co.nes.awssupport.business.bean.BillingCsvBean;
import jp.co.nes.awssupport.business.constant.LineItemType;
import jp.co.nes.awssupport.common.bean.ReservedManageBean;

/**
 * ElasticComputeCloud用の説明作成です。
 */
public class AmazonElasticComputeCloudDescriptionImpl extends BaseDescriptionImpl {

    /**
     * デフォルトコンストラクタです。
     */
    public AmazonElasticComputeCloudDescriptionImpl() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.business.logic.description.BaseDescriptionImpl#createDescriptionForReserved(jp.co.nes.awssupport.business.bean.BillingCsvBean, jp.co.nes.awssupport.common.bean.ReservedManageBean)
     */
    @Override
    protected String createDescriptionForReserved(BillingCsvBean csvBean, ReservedManageBean rmBean) {
        // TODO デフォルトは使用タイプでよいか？
        //String result = csvBean.getLineItemDescription();
        String result = csvBean.getUsageType();

        String productName = csvBean.getProductName();

        String regionCode = rmBean.getRegionCode();

        String reservedInstanceValue = getReservedInstanceValue(productName);
        if (reservedInstanceValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String operationValue = getOperationValueForReserved(productName, rmBean);
        if (operationValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String instanceType = rmBean.getInstanceType();
        if (instanceType == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String offeringClass = rmBean.getOfferingClass();
        String offeringClassValue = descriptionInfoHelper.get(productName, offeringClass);
        if (offeringClassValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String durationValue = descriptionInfoHelper.get(productName, Integer.toString(rmBean.getDuration()));
        if (durationValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String offeringTypeValue = descriptionInfoHelper.get(productName, rmBean.getOfferingType());
        if (offeringTypeValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        String lineItemTypeValue = getLineItemTypeValueForReserved(productName, csvBean.getLineItemType(),
                offeringClass);
        if (lineItemTypeValue == null) {
            addWarnMessage(csvBean);
            return result;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(getDescriptionPrefix(regionCode));
        builder.append(reservedInstanceValue);
        builder.append(FWC).append(operationValue);
        builder.append(FWC).append(instanceType);
        builder.append(FWC).append(offeringClassValue);
        builder.append(FWC).append(durationValue);
        builder.append(FWC).append(offeringTypeValue);
        builder.append(FWC).append(lineItemTypeValue);
        result = builder.toString();
        return result;
    }

    /**
     * 請求の種類に対応する日本語文字列を返却します(リザーブド用)。
     *
     * @param productName
     * @param lineItemType 請求の種類
     * @param offeringClass 要求クラス
     * @return 請求の種類に関する文字列
     */
    protected String getLineItemTypeValueForReserved(String productName, LineItemType lineItemType,
            String offeringClass) {
        final String EXCONVERTIBLE = "exconvertible";
        String result = null;
        if (EXCONVERTIBLE.equals(offeringClass)) {
            result = descriptionInfoHelper.get(productName, EXCONVERTIBLE);
        } else {
            result = super.getLineItemTypeValueForReserved(productName, lineItemType);
        }
        return result;
    }
}
